package com.jake.xpage.scheme;

import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XSchemeInterceptManager {
    @NonNull
    private final Map<String, IXSchemeInterceptor> mInterceptors = new ConcurrentHashMap<>();

    public void registerInterceptor(IXSchemeInterceptor interceptor) {
        if (interceptor == null) {
            return;
        }
        mInterceptors.put(interceptor.scheme(), interceptor);
    }

    public boolean handleIntercept(String url, Object extraObject) {
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        String scheme = Uri.parse(url).getScheme();
        IXSchemeInterceptor interceptor = mInterceptors.get(scheme);
        if (interceptor != null) {
            return interceptor.handleIntercept(url, extraObject);
        }
        return false;

    }
}
